package com.myCommunity.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.myCommunity.dao.CommentDao;
import com.myCommunity.entity.Comment;
@Repository("commentDao")
public class CommentDaoImpl extends BeseDao implements CommentDao{
	
	public List<Comment> getDetails(int topicId) {
		String hql="from Comment c where rownum <=4 and c.topic.id=? order by commentTime asc";
		List<Comment> list = (List<Comment>) getHibernateTemplate().find(hql,topicId);
		return list;
	}

	public List<Comment> getDetailsPage(final int topicId,  final int pageNo,final int pageSize) {
		return getHibernateTemplate().execute(new HibernateCallback<List<Comment>>() {
			public List<Comment> doInHibernate(Session session) throws HibernateException, SQLException {
				String hql="from Comment c where c.topic.id=?";
				Query query = session.createQuery(hql);
				query.setInteger(0, topicId);
				query.setFirstResult((pageNo-1)*pageSize);
				query.setMaxResults(pageSize);
				return query.list();
			}
		});
	}

	public void publishComment(Comment comment) {
		getHibernateTemplate().save(comment);
	}

}
